import { Component, Input } from '@angular/core';
import { NativeAudio } from "@ionic-native/native-audio";

@Component({
  selector: 'app-music',
  templateUrl: 'app-music.html'
})
export class AppMusicComponent {


  _track;

  @Input()
  get track() {

    return this._track
  }
  set track(val: any) {
    this._track = val
  }

  progress;
  ifPlaying = false;
  isFavorite = false;
  favoriteIcon = 'star-outline';
  player: object;
  isSeeking: boolean = false;

  constructor(public nativeAudio: NativeAudio) {

    this.ngOnInit();

  }
  /**
   * 初始加载
   */
  ngOnInit() {
  
 
    if (this.track) {
     
      let srcx =  this.track.src;
      console.log(srcx);

    }


  }
  /**
   * 切换歌曲
   */
  toggleSong() {

    if (this.ifPlaying) {
      this.stopSong();
    } else {
      this.playSong();
    }
  }
  /**
   * 播放
   */
  playSong() {
    // this.player.play();
    this.ifPlaying = true;

  }
  /**
   * 停止
   */
  stopSong() {
    this.ifPlaying = false;
    // this.player.stop()

  }
  /**
   * 进度
   */
  setProgress() {
    if (!this.isSeeking) {
      // let seek: any = this.player.seek();
      // this.progress = (((seek / this.player.duration()) * 100) || 0);
      requestAnimationFrame(this.setProgress.bind(this));
    }
  }
  /**
   * 设置播放声音的位置
   */
  seek(val) {
    this.isSeeking = false;
    // let duration = this.player.duration();
    // this.player.seek(duration * (val / 100));
    requestAnimationFrame(this.setProgress.bind(this));
  }

  handleDrag() {
    this.isSeeking = true;
  }
  /**
   * 播放结束
   */
  playerEnded() {
    this.ifPlaying = false;

  }

}
